import 'package:basic_common/common_exports.dart';
import 'package:basic_common/common_utils.dart';
import 'package:basic_patient/model/patient_family_list.dart';
import 'package:basic_patient/model/patient_single_data.dart';

class PatientDetailController extends GetxController {
  /// 菜单按钮
  final menuList = [
    {
      "icon": "assets/patient/icon_dangan.svg",
      "title": "健康档案",
      "color": "0xFF1877f2",
      "path": AppRoutes.patientHealth
    },
    {
      "icon": "assets/patient/icon_jilu.svg",
      "title": "健康记录",
      "color": "0xFFfa746b",
      "path": AppRoutes.healthRecord
    }
  ];

  final pData = PatientSingleData().obs;
  final familydataList = <PatientFamilyData>[].obs;
  final age = '0'.obs;

  /// 获取详情
  getPatientDetail() async {
    HttpData res = await ApiService().getPatientDetail(data: {
      "scene": "get-by-patientNo",
      "sceneDesc": "根据patientNo查询患者",
      "searchKey": "PatientNo",
      "searchValue": consumeNo.value
    });
    if (res.code == 1) {
      pData.value = PatientSingleData?.fromJson(res.data);
      update(['patientDetail']);
      String birthday = pData.value.birthday!;
      if (birthday.isNotEmpty && birthday.contains('-')) {
        List<String> list = birthday.split('-');
        age.value =
            CommonUtils.getAge(DateTime(int.parse(list[0] ?? '0'))).toString();
      }
    }
  }

  getPatientfamily() async {
    HttpData res = await ApiService()
        .getPatientFamilyList(data: {"consumerNo": consumeNo.value});
    if (res.code == 1) {
      List list = res.data;
      if (list.length > 0) {
        for (int i = 0; i < list.length; i++) {
          Map<String, dynamic> item = list[i];
          PatientFamilyData patientFamilyData =
              PatientFamilyData.fromJson(item);
          familydataList..add(patientFamilyData);
        }
      }
      update(['familyList']);
    }
  }

  String getRelation(String status) {
    String realName = '';
    switch (status) {
      case '1':
        realName = "本人";
        break;
      case '2':
        realName = "父母";
        break;
      case '3':
        realName = "子女";
        break;
      case '4':
        realName = "亲戚";
        break;
      case '5':
        realName = "朋友";
        break;
      case '6':
        realName = "其他";
        break;
    }
    return realName;
  }

  final consumeNo = ''.obs;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    consumeNo.value = Get.arguments['consumerNo'];
    getPatientDetail();
    getPatientfamily();
  }
}
